package dialer.client.web.controlador;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.RowEditEvent;

import dialer.ws.base.entity.Breakk;
import dialer.ws.base.exception.EntityException;
import dialer.ws.base.exception.EntityExistException;
import dialer.ws.business.service.BreakService;
import dialer.ws.common.util.FacesUtils;

@ManagedBean(name = "breakk")
@SessionScoped
public class BreakController implements Serializable {

	private static final long serialVersionUID = -7698170705928958302L;

	private static final Log log = LogFactory.getLog(BreakController.class);

	private String estado = "A";

	private List<Breakk> listBreak = new ArrayList<Breakk>();

	private Breakk selectedBreak = new Breakk();

	@ManagedProperty("#{breakService}")
	private BreakService breakService;

	public BreakController() {
		clear();
	}

	@PostConstruct
	public void init() {
		listBreak = breakService.findAll();
	}

	public void clear() {
		log.info("### clear");
		listBreak.clear();
		selectedBreak = new Breakk();
	}

	public void crear() {
		log.info("### crear" + selectedBreak);
		selectedBreak.setStatus("A");
		selectedBreak.setTipo("B");
		selectedBreak.setTipo("1");
		try {
			breakService.persist(selectedBreak);
			clear();
			init();
			FacesUtils.addInfoMessage("Satisfactorio", "se guardo correctamente");
		} catch (EntityExistException e) {
			log.error("### ERROR: " + e);
			FacesUtils.addErrorMessage("Error", "error");
		} catch (EntityException e) {
			log.error("### ERROR: " + e);
			FacesUtils.addErrorMessage("Error", "error");
		}
	}

	public void modificarBreak(RowEditEvent event) {
		Breakk breakEditado = (Breakk) event.getObject();

		Breakk breakModificado = null;
		try {
			breakModificado = (Breakk) breakService.merge(breakEditado);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Modificar Break: ", "Modifico Break Id: "
						+ breakModificado.getId()));
	}

	public String getEstado() {
		return estado;
	}

	public void setEstado(String estado) {
		this.estado = estado;
	}

	public BreakService getBreakService() {
		return breakService;
	}

	public void setBreakService(BreakService breakService) {
		this.breakService = breakService;
	}

	public List<Breakk> getListBreak() {
		return listBreak;
	}

	public void setListBreak(List<Breakk> listBreak) {
		this.listBreak = listBreak;
	}

	public Breakk getSelectedBreak() {
		return selectedBreak;
	}

	public void setSelectedBreak(Breakk selectedBreak) {
		this.selectedBreak = selectedBreak;
	}

}
